rm(list=ls())


# Check that required packages are installed:
want = c("foreign", "ggplot2", "MASS", "reshape2", "plyr", "dplyr", "ggrepel", 
         "lme4", "coda", "relaimpo", "relimp", "arm", "boot", "entropy", "here")
have = want %in% rownames(installed.packages())
if ( any(!have) ) { install.packages( want[!have] ) }
# load packages
junk <- lapply(want, library, character.only = TRUE)
rm(have,want,junk)

options(scipen=999)

#=============================================================================================================
# Load Image
load("allmodels.RData")
#=============================================================================================================
data$otherside_f <- ifelse(data$otherside_ch == 1, "Outgroup", "Ingroup")


#=============================================================================================================
# 1. Plots of Perceived + Experts Distances
#=============================================================================================================

# Mean perceived distance
dist_p_me <- as.data.frame(data %>% 
                             group_by(syslab, otherside_f) %>% 
                             dplyr::summarize(me = mean(dist_p, na.rm = T),
                                              ri_l2_m = mean(ri_l2_m)))
# Mean experts' distance
dist_ch_me <- as.data.frame(data %>% 
                              group_by(syslab, otherside_f) %>% 
                              dplyr::summarize(me = mean(dist_ch, na.rm = T),
                                               ri_l2_m = mean(ri_l2_m)))

quartz(type = 'pdf', file = 'obj_perc_dist_ingroup.pdf',
       width=10, height=8, dpi=300)
cols <- c("Experts" = "blue", "Perceived" = "red")
ggplot(subset(data, otherside_f == "Ingroup")) +
  geom_density(aes(x = dist_p, col = "Perceived"), adjust = 3, linetype = "solid") +
  geom_vline(data = subset(dist_p_me, otherside_f == "Ingroup"), 
             aes(xintercept = me, col = "Perceived"),
             linetype="dashed") +
  geom_density(aes(x = dist_ch, col = "Experts"), adjust = 3, linetype = "solid") +
  geom_vline(data = subset(dist_ch_me, otherside_f == "Ingroup"), 
             aes(xintercept = me, col = "Experts"),
             linetype="dashed") +
  scale_colour_manual(name="", values = cols) +
  facet_wrap(~reorder(syslab, ri_l2_m)) +
  scale_x_continuous(breaks = seq(0, 10, 1)) +
  theme_bw() +
  ggtitle("Distance from Ingroup parties") +
  xlab("Left-Right distance respondent/party") +
  theme(legend.position = c(0.9, 0.1))
dev.off()

quartz(type = 'pdf', file = 'obj_perc_dist_outgroup.pdf',
       width=10, height=8, dpi=300)
cols <- c("Experts" = "blue", "Perceived" = "red")
ggplot(subset(data, otherside_f == "Outgroup")) +
  geom_density(aes(x = dist_p, col = "Perceived"), adjust = 3, linetype = "solid") +
  geom_vline(data = subset(dist_p_me, otherside_f == "Outgroup"), 
             aes(xintercept = me, col = "Perceived"),
             linetype="dashed") +
  geom_density(aes(x = dist_ch, col = "Experts"), adjust = 3, linetype = "solid") +
  geom_vline(data = subset(dist_ch_me, otherside_f == "Outgroup"), 
             aes(xintercept = me, col = "Experts"),
             linetype="dashed") +
  scale_colour_manual(name="", values = cols) +
  facet_wrap(~reorder(syslab, ri_l2_m)) +
  scale_x_continuous(breaks = seq(0, 10, 1)) +
  theme_bw() +
  ggtitle("Distance from Outgroup parties") +
  xlab("Left-Right distance respondent/party") +
  theme(legend.position = c(0.9, 0.1))
dev.off()